#pragma once

#ifdef _WINDOWS
#include <WinSock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#endif

#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
class MyHttpfile
{
public:
	//typedef std::vector<std::string> 
	typedef std::multimap<std::string,std::string> MKV;//muliti key - val

	typedef struct _tagURL
	{
		std::string str;
		std::string proto,host,uri;		
	}URL;
	typedef unsigned char byte;
	typedef unsigned short word;
	typedef unsigned long dword;
	typedef unsigned long long ddword;
	typedef struct _tagHTTPHEADER
	{
		std::string str;
		std::string ver,uri,tip,rescode,host;
		dword content_length;
		MKV property;
	}HTTPHEADER;

#define DEFAULT_PORT "80"
#define DEFAULT_URI "/"
#define HTTP_SPACE " "
#define HTTP_GET "GET"HTTP_SPACE
#define HTTP_POST "POST"HTTP_SPACE
#define HTTP_VER "HTTP/1.1"
#define HTTP_ENDLINE "\r\n"
#define HTTP_HOST "Host:"HTTP_SPACE
#define HTTP_ACCEPT "Accept:"HTTP_SPACE
#define MAX_RECVBUF 1024

protected:
	HTTPHEADER m_sendheader,m_responseheader;
	SOCKET m_socket;
	std::string m_host,m_port;
	std::string m_rescontent,m_recvtmp;
	
	std::stringstream m_recvsstream;
	std::fstream m_recvfstream;
	std::istream *m_resstream;

	bool parseurl(const std::string &url);
	bool connect();
	bool sendheader();
	bool receive();
	std::string &recline();
public:
	
	MyHttpfile(void);
	~MyHttpfile(void);
	bool open(const std::string &url);
	bool perform();
	const std::string &rescontent() ;
	std::istream &resstream();
};

#ifdef _WINDOWS
extern WSAData wsaData;
#endif
